﻿'use strict';

app
  .run(
      function ($window,$rootScope,   $state,   $stateParams,$localStorage,$http,$location) {
		  $http.defaults.headers.common['Authorization'] = 'Bearer ' + $localStorage.token;
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;
          $rootScope.$on('$stateChangeStart', function ($scope,event, to, toParams, from, fromParams) {
              if ((!!to.url && '/login;/loading;'.indexOf(to.url) < 0) || $window.sessionStorage["loginToken"]==null) {
                  $location.path("/auth/login");
              }
          });
          $rootScope.$on('$stateChangeSuccess', function(event, to, toParams, from, fromParams) {
            $rootScope.previousState = from;
            $rootScope.previousStateParams = fromParams;
          });
	}
  )
.config(
      function ($stateProvider,   $urlRouterProvider) {
          $urlRouterProvider
              .otherwise('/auth/loading');
          $stateProvider
              .state('auth',{
                  abstract: true,
                  url:'/auth',
                  template: '<div ui-view class="fade-in"></div>',
                  resolve: {
                      deps: ['$ocLazyLoad',
                        function( $ocLazyLoad ){
                          return $ocLazyLoad.load('admin/auth/ctrl.js');
                      }]
                  }
              })
              .state('auth.loading',{
                  url:'/loading',
                  templateUrl:'admin/auth/loading.html',
              })
              .state('auth.login',{
                  url:'/login',
                  templateUrl:'admin/auth/login.html',
              })
		  
              .state('app', {
                  abstract: true,
                  url: '/app',
                  templateUrl: 'admin/app.html',
              })
             .state('app.dashboard', {
                 url: '/dashboard',
                 templateUrl: 'admin/dashboard.html',
                 ncyBreadcrumb: {
                   label: '<i class="fa fa-home"></i> 首页'
                 }
             })

              // .state('app.dashboard', {
              //     abstract: true,
              //     url: '/dashboard',
              //     template: '<div ui-view class="fade-in"></div>',
              //     resolve: {
              //         deps: ['$ocLazyLoad',
              //             function( $ocLazyLoad ){
              //                 return $ocLazyLoad.load('admin/homePage/ctrl.js');
              //             }]
              //     }
              // })
              // .state('app.dashboard.list', {
              //     url: '/getAmount',
              //     templateUrl: 'admin/dashboard.html',
              //     ncyBreadcrumb: {
              //         parent:'app.dashboard',
              //         label: '首页列表',
              //     }
              // })



              .state('app.banner', {
                  abstract: true,
                  url: '/banner',
                  template: '<div ui-view class="fade-in"></div>',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load('admin/banner/ctrl.js');
                          }]
                  }
              })
              .state('app.banner.list', {
                  url: '/list?page&search',
                  templateUrl: 'admin/banner/list.html',
                  ncyBreadcrumb: {
                      parent:'app.dashboard',
                      label: 'banner图',
                  }
              })
              .state('app.banner.detail', {
                  url: '/banner/getOne/{id}',
                  templateUrl: 'admin/banner/detail.html',
                  ncyBreadcrumb: {
                      parent:'app.banner.list',
                      label: '编辑',
                  }
              })
              .state('app.banner.create', {
                  url: '/banner/save',
                  templateUrl: 'admin/banner/detail.html',
                  ncyBreadcrumb: {
                      parent:'app.banner.list',
                      label: '新增',
                  }
              })
              .state('app.feedback', {
                  abstract: true,
                  url: '/queryFeedbackList',
                  template: '<div ui-view class="fade-in"></div>',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load('admin/feedback/ctrl.js');
                          }]
                  }
              })
              .state('app.feedback.list', {
                  url: '/feedback/queryFeedbackList?page&search',
                  templateUrl: 'admin/feedback/list.html',
                  ncyBreadcrumb: {
                      parent:'app.dashboard',
                      label: '意见反馈',
                  }
              })
              .state('app.feedback.detail', {
                  url: '/feedback/queryFeedback/{id}',
                  templateUrl: 'admin/feedback/detail.html',
                  ncyBreadcrumb: {
                      parent:'app.feedback.list',
                      label: '查看',
                  }
              })


              .state('app.complaints', {
                  abstract: true,
                  url: '/complaints/queryComplaintsList',
                  template: '<div ui-view class="fade-in"></div>',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load('admin/complaint/ctrl.js');
                          }]
                  }
              })
              .state('app.complaints.list', {
                  url: '/complaints/queryComplaintsList?page&search',
                  templateUrl: 'admin/complaint/list.html',
                  ncyBreadcrumb: {
                      parent:'app.dashboard',
                      label: '投诉管理',
                  }
              })
              .state('app.complaints.detail', {
                  url: '/complaints/queryComplaints/{id}',
                  templateUrl: 'admin/complaint/detail.html',
                  ncyBreadcrumb: {
                      parent:'app.complaints.list',
                      label: '编辑',
                  }
              })

              .state('app.withdraws', {
                  abstract: true,
                  url: '/withdraws',
                  template: '<div ui-view class="fade-in"></div>',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load('admin/withdraws/ctrl.js');
                          }]
                  }
              })
              .state('app.withdraws.list', {
                  url: '/list?page&search',
                  templateUrl: 'admin/withdraws/list.html',
                  ncyBreadcrumb: {
                      parent:'app.dashboard',
                      label: '提现列表',
                  }
              })

		}
  );

app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('AuthInterceptor');
})
app.factory('AuthInterceptor', function ($window,$rootScope, $q, $location) {
    return {
        responseError: function (response) {

            if (response.status == 401) {
                $location.url('/auth/login');
            }
            return $q.reject(response);
        }
    };
})